#include "CyDelegate.h"
#include <Windows.h>

#pragma once
class CyTimer
{
private:
	CyDelegate_base * _handler;
	unsigned int _delay;
	UINT_PTR _id;
	HWND _hWnd;

public:
	CyTimer(int delay = 1000);
	~CyTimer(void);

	CyTimer& SetDelay(unsigned int delay);
	CyTimer& Start();
	CyTimer& Stop();

	// set handler for timer event
	template <class R> void SetHandler(R * obj,
		typename CyDelegate<R>::Handler handler)
	{
		if (obj == NULL || handler == NULL)
		{
			if(_handler != NULL)
				delete _handler;
			_handler = NULL;
		}
		else
		{
			_handler = new typename CyDelegate<R>(obj, handler);
		}
	}

	friend LRESULT CALLBACK CyTimerProc(HWND hwnd,
		UINT message, WPARAM wParam, LPARAM lParam);
};

